--[[
FilePath: \Lua\View\UISoloBattle\UIEnterSolo.lua
1v1选择英雄
--]]

require 'View/UIBase/UIBase'

UIEnterSolo = UIBase.New(PanelNames.UIEnterSolo)
local _M = UIEnterSolo

_M.linkColor = "#" .. G.dataTable["t_global"][tostring(185)]["f_string_value"];
_M.mainColor = "#" .. G.dataTable["t_global"][tostring(237)]["f_string_value"];
_M.hasInit = false;
_M.selectType = 0;
_M.tableHeros = nil;
_M.typeHeros = nil;
_M.selectHeroItem = nil;
_M.selectNum = 0;
_M.isPlay = false;

function _M.Init(func)
    UIBase.Init(_M, func);
end

function _M:OnCreate()
    if not _M.hasInit then
        self.btnClose = self:GetChild("Btn_BgClose");
        self.title = self:GetChild("Com_TwoBg"):GetChild("title");
        self.tips1 = self:GetChild("Text_SoloTips01");
        self.tips2 = self:GetChild("Text_SoloTips02");
        self.listHero = self:GetChild("List_Hero");
        self.listSelectHero = self:GetChild("List_SelectHero");
        self.tabCtrl = self:GetController("ctrlTab");
        self.tab = {};
        for i = 0, 6 do
            table.insert(self.tab, self:GetChild("Tab"..i));
        end
        self.btnPlay = self:GetChild("Btn_Play");
        self.btnHelp = self:GetChild("Btn_Help");

        --self.listHero.itemRenderer = self.listHeroItem;
        self.listSelectHero.itemRenderer = self.listSelectHeroItem;
        self.listSelectHero:SetVirtual();

        _M.hasInit = true;
        self.mainReward = self:GetChild("mainReward")
        logicMgr.FuncOpenManager.refreshMainReward(self.mainReward,Config.FuncOpenType.RealMan)
        self:addEvent();
    end
    self.tabCtrl.selectedIndex = 0;
    self.selectType = 0;
    self:refreshView();
end

function _M:addEvent()
    self.btnClose.onClick:Add(self.onClosePanel,self);
    self.btnPlay.onClick:Add(self.onPlay,self);
    self.btnHelp.onClick:Add(self.onBtnHelp, self);
    for i = 1, #self.tab do
        self.tab[i].onClick:Add(function()
            _M:selectHeroType(i - 1);
        end);
    end
end

function _M:addLocalEvent()
    self:RegisterEvent(Config.EventType.Fresh_RealManFormation,self.realManFormation);
    self:RegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeEnterSolo);
end

function _M:removeEvent()
    self.btnClose.onClick:Remove(self.onClosePanel);
    self.btnPlay.onClick:Remove(self.onPlay);
    self.btnHelp.onClick:Remove(self.onBtnHelp);
end

function _M:removeLocalEvent()
    self:UnRegisterEvent(Config.EventType.Fresh_RealManFormation,self.realManFormation);
    self:UnRegisterEvent(Config.EventType.Fresh_RealManRealManWar,self.closeEnterSolo);
end

function _M:onBtnHelp()
    if self.isBattle then
        return;
    end
    logicMgr.UIShowManager.ShowCommonTips("SoloBattle_Info_Title", "SoloBattle_Info_Tips", "FunctionStory311");
end

function _M:onPlay()
    if self.isPlay then
        return;
    end
    --if not logicMgr.RealManWarManager.isOpen() then
    --    UISysTips.AddMsg(getLanguage("战斗结束"));
    --    return;
    --end
    if self.selectNum < 1 then
        UISysTips.AddMsg(getLanguage("DarkLaneTipes_8"));
        return;
    end
    if self.selectNum < 5 then
        local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
        pop.Init("67", function(b)
            if b then
                self:setFormation();
            end
        end);
        return;
    end
    self:setFormation();
end

function _M:realManFormation(isSuc)
    if isSuc then
        self:onClosePanel();
        logicMgr.RealManWarManager.OpenSoloBattle();
    else
        self.isPlay = false;
    end
end

function _M:closeEnterSolo()
    self:onClosePanel();
end

function _M:setFormation()
    self.isPlay = true;
    local formation = {};
    for i = 1, #self.selectHeroItem do
        if self.selectHeroItem[i].hero ~= nil then
            table.insert(formation, self.selectHeroItem[i].hero.partnerId);
        end
    end
    RealManWarHandler.sendReqSetRealManFuckFormationMessage(formation);
end

function _M:refreshView()
    self.title.text = getLanguage("SoloBattle_Title");
    self.tips1.text = getLanguage("SoloBattle_EnterTips01");
    self.tips2.text = getLanguage("SoloBattle_EnterTips02");
    self.selectHeroItem = {};
    self:updateSelectHeroItem();
    self.listHero:RemoveChildrenToPool();
    for i = 1, 5 do
        local itemData = {};
        itemData.hero = nil;
        local item = self.listHero:AddItemFromPool();
        item:GetController("state").selectedIndex = 0;
        itemData.item = item;
        itemData.item.onClick:Clear();
        itemData.item.onClick:Add(function()
            if itemData.hero ~= nil then
                itemData.hero = nil;
                itemData.item:GetController("state").selectedIndex = 0;
                if _M.selectNum > 0 then
                    _M.selectNum = _M.selectNum - 1;
                end
                self.listSelectHero:RefreshVirtualList();
            end
        end);
        table.insert(self.selectHeroItem, itemData);
    end
end

function _M:selectHeroType(index)
    if self.selectType == index then
        return;
    end
    self.selectType = index;
    self:updateSelectHeroItem();
end

function _M:updateSelectHeroItem()
    if self.tableHeros == nil then
        self.tableHeros = logicMgr.HeroManager.getOwnerHeroList();
        local HeroSortType=logicMgr.HeroManager.HeroSortType
        local sort=logicMgr.HeroManager.sortHeroWithPower
        if HeroManager.sortType==HeroSortType.power then
            sort=logicMgr.HeroManager.sortHeroWithPower
        elseif HeroManager.sortType==HeroSortType.quality then
            sort=logicMgr.HeroManager.sortHeroWithQuality
        else
            sort=logicMgr.HeroManager.sortHeroWithLevel
        end
        table.sort(self.tableHeros, sort);
        --table.sort(self.tableHeros, function(a, b)
        --    if a["level"] > b["level"] then
        --        return true;
        --    elseif a["level"] == b["level"] then
        --        if a["star"] > b["star"] then
        --            return true;
        --        elseif a["star"] == b["star"] then
        --            return a["partnerModelId"] < b["partnerModelId"];
        --        else
        --            return false;
        --        end
        --    else
        --        return false;
        --    end
        --end);
    end
    self.typeHeros = {};
    for i = 1, #self.tableHeros do
        if self.selectType == 0 or self.tableHeros[i].type == self.selectType then
            table.insert(self.typeHeros, self.tableHeros[i]);
        end
    end
    self.listSelectHero.numItems = #self.typeHeros;
    HelperFunc.ListTranstionPlay(self.listSelectHero)
end

function _M.listHeroItem(index, itemObj)
    local heroItem = itemObj:GetChild("Btn_Hero");
    local state = itemObj:GetController("state");
    state.selectedIndex = 0;
end

function _M.listSelectHeroItem(index, itemObj)
    local hero = _M.typeHeros[index + 1];
    local ctrlHadBattle = itemObj:GetController("ctrlHadBattle")
    local charactor = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    if charactor ~= nil and hero ~= nil then
        charactor.initData(itemObj, hero);
        charactor.updateDecTitle(itemObj, hero, 0, nil, EnumConst.FightTypeConst.RealMan);
    end
    local isSelect = _M:isSelect(hero);
    if isSelect then
        ctrlHadBattle.selectedIndex = 1;
    else
        ctrlHadBattle.selectedIndex = 0;
    end
    itemObj.onClick:Clear();
    itemObj.onClick:Add(function()
        if isSelect then
            isSelect = false;
            ctrlHadBattle.selectedIndex = 0;
            if _M.selectNum > 0 then
                _M:removeHero(hero);
                _M.selectNum = _M.selectNum - 1;
            end
        else
            if _M.selectNum >= 5 then
                UISysTips.AddMsg(getLanguage("Character_Tips1"));
            else
                isSelect = true;
                _M.selectNum = _M.selectNum + 1;
                ctrlHadBattle.selectedIndex = 1;
                _M:selectHero(hero);
            end

        end
    end);
end

function _M:selectHero(hero)
    for i = 1, #self.selectHeroItem do
        if self.selectHeroItem[i].hero == nil then
            local itemObj = self.selectHeroItem[i].item;
            itemObj:GetController("state").selectedIndex = 1;
            local charactor = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
            if charactor ~= nil and hero ~= nil then
                local obj = itemObj:GetChild("Btn_Hero");
                charactor.initData(obj, hero);
                charactor.updateDecTitle(obj, hero, 0, nil, EnumConst.FightTypeConst.RealMan);
            end
            self.selectHeroItem[i].hero = hero;
            break;
        end
    end
end

function _M:removeHero(hero)
    for i = 1, #self.selectHeroItem do
        if self.selectHeroItem[i].hero ~= nil and self.selectHeroItem[i].hero.partnerId == hero.partnerId then
            local itemObj = self.selectHeroItem[i].item;
            itemObj:GetController("state").selectedIndex = 0;
            self.selectHeroItem[i].hero = nil;
            break;
        end
    end
end

function _M:isSelect(hero)
    for i = 1, #self.selectHeroItem do
        if self.selectHeroItem[i].hero ~= nil and self.selectHeroItem[i].hero.partnerId == hero.partnerId then
            return true;
        end
    end
    return false;
end

-- 显示
function _M:OnEnable()
    self:addLocalEvent();
end

function _M:Refresh()

end

-- 隐藏
function _M:OnDisable()
    self:removeLocalEvent();
end

function _M:onClosePanel()
    closeUI(PanelNames.UIEnterSolo);
end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    self.hasInit = false;
    self.tableHeros = nil;
    self.selectHeroItem = nil;
    self.selectNum = 0;
    self.isPlay = false;
end

return _M;